package com.yuanye.gwcit.fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.yuanye.gwcit.R;
import com.yuanye.yeecho.base.BaseFragment;


public class BrightFragment extends BaseFragment {

    String tag = "BrightFragment";

    private Button bright_up;
    private Button bright_down;
    boolean toMin = false;
    boolean toMax = false;
    private ProgressBar progressBar;
    private int currentBrightness;
    private int defaultBrightness;

    public BrightFragment() {
        setTaskName("亮度调节");
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_bright, container, false);
        progressBar = view.findViewById(R.id.progressBar);
        progressBar.setMax(255);
        progressBar.setMin(10);
        currentBrightness = Settings.System.getInt(requireActivity().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 125);
        defaultBrightness = currentBrightness;
        progressBar.setProgress(currentBrightness);
        bright_up = view.findViewById(R.id.bright_up);
        bright_down = view.findViewById(R.id.bright_down);
        bright_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                increaseBrightness(50);
            }
        });
        bright_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decreaseBrightness(50);
            }
        });

        return view;
    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (toMin && toMax){
//                btnPass.setVisibility(View.VISIBLE);
                AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
                builder.setTitle("询问")
                        .setMessage("测试是否通过？")
                        .setPositiveButton("通过", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                passAndNext();

                            }
                        })
                        .setNegativeButton("失败", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                setFail();
                            }
                        })
                        .show();
            }
        }
    };

    public void increaseBrightness(int delta) {
        currentBrightness = currentBrightness + delta;
        if ( currentBrightness > 255){
            currentBrightness = 255;
            toMax = true;
            handler.sendEmptyMessage(0);
        }
        progressBar.setProgress(currentBrightness);
        setAppBrightness(currentBrightness/255f);
    }

    public void decreaseBrightness(int delta) {
        currentBrightness = currentBrightness - delta;
        if (currentBrightness < 10){
            currentBrightness = 10;
            toMin = true;
            handler.sendEmptyMessage(0);
        }
        progressBar.setProgress(currentBrightness);
        setAppBrightness(currentBrightness/255f);

    }

    private void setAppBrightness(float brightness) {

        WindowManager.LayoutParams layoutParams = requireActivity().getWindow().getAttributes();
        layoutParams.screenBrightness = brightness;
        requireActivity().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void execTask() {
        CountDownTimer timer = new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long l) {
                if (toMin && toMax){
                    cancel();
                    Log.d(tag, "onTick: cancel");
                }else{
                    if (!toMin){
                        decreaseBrightness(300);
                        Log.d(tag, "onTick: set to min");
                    }else{
                        increaseBrightness(50);
                        Log.d(tag, "onTick: increase");
                    }
                }

            }

            @Override
            public void onFinish() {

            }
        };
        timer.start();
    }

    @Override
    public void stopTask() {
        setAppBrightness(defaultBrightness);
    }
}
